// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    list: [],
    showMore: false,
    companyList: [],
    pagination: {
      page_start: 1,
      page_size: 10
    },
    enterprise_id: "", // 企业id
    start_time: "",    
    end_time: "",
    default_date: "", // 初始化日期
    default_date1: "",
    sum_money: 0,
  },
  changeCard: function (e) {
    let {
      id
    } = e.currentTarget.dataset || {};
    wx.navigateTo({
      url: '/pages/changeCard/changeCard?id=' + this.data.id,
    })
  },
  // 查看签约
  toSign1(e) {
    let {
      id
    } = e.currentTarget.dataset || {};
    wx.navigateTo({
      url: '/pages/business/business?id=' + this.data.id,
    })
  },
  // 签署报告
  sign_report(e) {
    let {
      id
    } = e.currentTarget.dataset || {};
    wx.navigateTo({
      url: '/pages/signedReport/signedReport?id=' + this.data.id,
    })
  },
  setPage() {
    this.setData({
      "pagination.page_start": 1,
      "pagination.page_size": 10
    })
  },
  getDate(date, type) {
    const year = type ? date.getFullYear() - 10 : date.getFullYear(),
      month = date.getMonth() + 1,
      day = date.getDate(),
      hour = date.getHours(),
      minute = date.getMinutes(),
      second = date.getSeconds();
    return year + '-' + month;
  },
  month_change(e) {
    console.log(e)
    let {
      value
    } = e.detail || {}, {
      type
    } = e.currentTarget.dataset;
    console.log(value)
    if (type == 0) {
      this.setData({
        start_time: value,
        default_date: parseInt(value.split('-')[0]) + '年' + parseInt(value.split('-')[1]) + '月',
      })
    } else {
      this.setData({
        end_time: value,
        default_date1: parseInt(value.split('-')[0]) + '年' + parseInt(value.split('-')[1]) + '月',
      })
    }

    this.setPage();
    this.getData();
  },
  enterprise_change(e) {
    let {
      value
    } = e.detail || {}, list = this.data.companyList;
    let data = list[Number(value)];
    console.log(e, data)
    this.setData({
      enterprise_id: data.enterprise_id,
      enterprise_name: data.enterprise_name
    })
    this.setPage();
    this.getData();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      this.setData({
        id: options.id,
        enterprise_id: options.enterprise_id
      })
    }
    // 默认时间
    let date = new Date();
    let year = date.getFullYear();
    let year1 = date.getFullYear() - 1;
    let month = date.getMonth() + 1;
    this.setData({
      default_date: year1 + '年' + month + '月',
      default_date1: year + '年' + month + '月',
      start_time: year1 + '-' + month ,
      end_time: year + '-' + month ,
      list: [],
      page_no: 1
    })
    this.getData();
  },
  replaceTime(time) {
    let date = time.replace(/-/g, "/"); //替换字符，变成标准格式 
    return new Date(Date.parse(date));
  },
  async getContractEnterprises() {
    let data = await wx.$api.getContractEnterprises({});
    console.log("企业列表", data)
    this.setData({
      companyList: data.data.data || []
    })
  },
  async getData() {
    let data = await wx.$api.getAccountLog({
      pagination: this.data.pagination,
      enterprise_id: this.data.enterprise_id, // 企业id
      start_time: this.data.start_time,
      end_time: this.data.end_time
    });
    console.log('my_data', data);
    this.setData({
      list: data.data.data.list || [],
      sum_money: data.data.data.sum_money || 0
    })
  },
  onTabItemTap() {
    this.getData();
  },
  bindscrolltolower() {
    this.setData({
      "pagination.page_start": Number(this.pagination.page_start) + 1,
    })
    this.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})